﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AppFx.Console
{
    /// <summary>
    /// Delegate used to validate a parameter.
    /// </summary>
    /// <param name="parameter">Parameter to validate.</param>
    /// <returns>A bool indicating if the validation was successfull.</returns>
    public delegate bool ValidateDelegate(ConsoleParameter parameter);

    /// <summary>
    /// Represents a parameter for a ConsoleCommand.
    /// </summary>
    public class ConsoleParameter
    {
        #region Properties

        /// <summary>
        /// Gets or sets if the parameter is optional. (default: false)
        /// </summary>
        public bool Optional { get; set; }

        /// <summary>
        /// Gets or sets the name of the parameter.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the Validator used for validation of the Parameter.
        /// </summary>
        public ValidateDelegate Validator { get; set; }

        /// <summary>
        /// Gets or sets the text that is displayed for invalid input.
        /// </summary>
        public string ValidationMessage { get; set; }

        /// <summary>
        /// Gets or sets the value of the parameter.
        /// </summary>
        public string Value { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a new instance of the ConsoleParameter-class.
        /// </summary>
        /// <param name="name">Name to use for the parameter.</param>
        public ConsoleParameter(string name, ValidateDelegate validator, string validationMessage)
        {
            Name = name;
            Validator = validator;
            ValidationMessage = validationMessage;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Validates the input.
        /// </summary>
        /// <returns>A bool indicating if the validation was successful.</returns>
        public bool Validate()
        {
            if (Validator.Invoke(this))
                return true;
            else
                return false;
        }

        #endregion
    }
}
